<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>协同过滤景点推荐系统</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <link rel="stylesheet" href="${request.contextPath}/statics/css/bootstrap.min.css">
  <link rel="stylesheet" href="${request.contextPath}/statics/css/font-awesome.min.css">
  <link rel="stylesheet" href="${request.contextPath}/statics/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="${request.contextPath}/statics/css/all-skins.min.css">
  <link rel="stylesheet" href="${request.contextPath}/statics/css/main.css">
  <script src="${request.contextPath}/statics/libs/jquery.min.js"></script>
  <script src="${request.contextPath}/statics/plugins/layer/layer.js"></script>
  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>
<body class="hold-transition login-page">
<div class="login-box" id="rrapp" v-cloak>
  <div class="login-logo">
    <b>协同过滤景点推荐系统</b>
  </div>
  <!-- /.login-logo -->
  <div class="login-box-body">
      <p class="login-box-msg">登录</p>
      <div v-if="error" class="alert alert-danger alert-dismissible">
        <h4 style="margin-bottom: 0px;"><i class="fa fa-exclamation-triangle"></i> {{errorMsg}}</h4>
      </div>
      <div class="form-group has-feedback">
        <input type="text" class="form-control" v-model="username" placeholder="账号">
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">
        <input type="password" class="form-control" v-model="password" placeholder="密码">
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">
        <input type="text" class="form-control" v-model="captcha" @keyup.enter="login" placeholder="验证码">
        <span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">
        <img alt="如果看不清楚，请单击图片刷新！" class="pointer" :src="src" @click="refreshCode">
        &nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:;" @click="refreshCode">点击刷新</a>
      </div>
      
      
      <div class="row">
        <div class="col-xs-4">
          <div class="checkbox icheck">
          </div>
        </div>
        <!-- /.col -->
        <div class="col-xs-4">
          <button type="button" class="btn btn-primary btn-block btn-flat" @click="login">登录</button>
          
        </div>
        <div class="col-xs-4">
          <button type="button" class="btn btn-primary btn-block btn-flat" @click="register">注册</button>
        </div>
        <!-- /.col -->
      </div>
    <!-- /.social-auth-links -->

  </div>
  <!-- /.login-box-body -->
  <div id="registerform" style="display: none;">
    <div id="openContent" style="margin: 20px;">
      <div class="form-group has-feedback">
        <input type="text" name="username" class="form-control" v-model="user.username" placeholder="账号">
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">
        <input type="password" name="password" class="form-control" v-model="user.password" placeholder="密码">
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
      </div>
    </div>
  </div>
</div>
<!-- /.login-box -->


<script src="${request.contextPath}/statics/libs/jquery.min.js"></script>
<script src="${request.contextPath}/statics/libs/vue.min.js"></script>
<script src="${request.contextPath}/statics/libs/bootstrap.min.js"></script>
<script src="${request.contextPath}/statics/libs/jquery.slimscroll.min.js"></script>
<script src="${request.contextPath}/statics/libs/fastclick.min.js"></script>
<script src="${request.contextPath}/statics/libs/app.js"></script>
<script src="${request.contextPath}/statics/libs/bootstrapValidator.js"></script>
<script type="text/javascript">
var vm = new Vue({
	el:'#rrapp',
	data:{
		username: '',
		password: '',
		captcha: '',
		error: false,
		errorMsg: '',
		src: 'captcha.jpg',
    user:{
      username:'',
      password:'',
    }
	},
	beforeCreate: function(){
		if(self != top){
			top.location.href = self.location.href;
		}
	},
	methods: {
    register:function(){
      $("#openContent").show()
      layer.open({
        type: 1,
        title: "注册账号",
        content: $("#registerform"),
        area: ['350px', '240px'],
        offset: 'auto',
        btn: ['注册','取消'],
        closeBtn: 0,
        btn1: function (index, layero) {
          $("#openContent").hide()
          vm.registerRequest()
          layer.close(index);
        },
        btn2: function (index, layero) {
          $("#openContent").hide()
          layer.close(index);
        }
      });
      this.formatterObj()  
    },
		refreshCode: function(){
			this.src = "captcha.jpg?t=" + $.now();
		},
    registerRequest: function() {
      var bvObj = $("#openContent").data("bootstrapValidator");
      bvObj.validate()
      const res = bvObj.isValid();
      if (res) {
        vm.user.status = 1,
        vm.user.email = 'default@email.com'
        vm.user.roleIdList = [2];
        $.ajax({
          type: "POST",
          contentType: "application/json",
          url: "sys/user/save",
          data: JSON.stringify(vm.user),
          dataType: "json",
          success: function(result){
            if(result.code == 0){//登录成功
              alert("注册成功")
              // parent.location.href ='index.html';
            }else{
              vm.error = true;
              vm.errorMsg = result.msg;
              
              vm.refreshCode();
            }

          }
        });
      }
    },
    formatterObj: function() {
      $('#openContent').bootstrapValidator({
        // 默认的提示消息
        message: 'This value is not valid',
        live: 'enabled',
        // 表单框里右侧的icon
        feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
        },
        submitHandler: function (validator, form, submitButton) {
          // 表单提交成功时会调用此方法
          // validator: 表单验证实例对象
          // form  jq对象  指定表单对象
          // submitButton  jq对象  指定提交按钮的对象
        },
        fields: {
          username: {
            validators: {
              notEmpty: {
                message: '用户名不能为空'
              },
              regexp: {
                regexp: /^[A-Za-z0-9]+$/,
                message: '只能用英文或数字作为用户名'
              }
            }
          },
          password: {
            validators: {
              notEmpty: {
                message: '密码不能为空'
              },
              regexp: {
                regexp: /^[A-Za-z0-9]+$/,
                message: '只能用英文或数字作为密码'
              }
            }
          }
        }
      });
    },
		login: function (event) {
			var data = "username="+vm.username+"&password="+vm.password+"&captcha="+vm.captcha;
			$.ajax({
				type: "POST",
			    url: "sys/login",
			    data: data,
			    dataType: "json",
			    success: function(result){
					if(result.code == 0){//登录成功
            if(result.role == 2){
              parent.location.href = "/modules/senic/senicuserview.html"
            }else{
              parent.location.href ='index.html';
            }
					}else{
						vm.error = true;
						vm.errorMsg = result.msg;
						
						vm.refreshCode();
					}
				}
			});
		}
	}
});
</script>
</body>
<style>
  .glyphicon-ok {
    margin-right: 20px;
  }
  .glyphicon-remove {
    margin-right: 20px;
  }
</style>
</html>
